import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col } from 'antd';
import { FormGroup, DATA_ENTRY, codeType, UploadC, FormItemLayout } from '../../../../comps';
import styles from '../style.less'
import config from '@/src/utils/config';
@Form.create()
@connect(({ valuationAnalysisM }) => ({
  valuationAnalysisM,
}))
export default class AddAnalysis extends PureComponent {
  state = {
    dimensionList: [
      { value: '1', title: '产品' },
      { value: '2', title: '资产' },
    ],
    assetTypeList: [
      { value: '01', title: '资产管理计划' },
      { value: '02', title: '委外资产' },
      { value: '03', title: '基金' },
    ],
    assetType: '01',
    assetDict: {
      '01': 'zgtzptPlan',
      '02': 'outsourcing',
      '03': 'smjj',
    },
    selectProduct: undefined,
    isStandard: '1',
  };
  componentDidMount() {
    const { infoDetail } = this.props;
    this.props.onRef(this);
    if (infoDetail) {
      this.setState({
        dimension: infoDetail.dimension,
        assetType: infoDetail.assetType,
      });
    }
  }
  // 查询维度/资产类型变化
  formFieldChange = (type, e) => {
    const {
      form: { setFieldsValue },
    } = this.props;
    this.setState(
      {
        [type]: e || undefined,
      },
      () => {
        setFieldsValue({
          code: undefined,
        });
      },
    );
  };
  // 提供给外部获取表单信息
  getInfo = () => {
    const { selectTranding } = this.state;
    const {
      form: { validateFields },
      valuationAnalysisM: { productList = [] },
    } = this.props;
    let params = undefined;
    validateFields((err, values) => {
      if (!err) {
        params = values;
        if (params.dimension == '1') {
          params.codeId = productList.filter((item) => item.code === params.code)[0]?.id || '';
          params.codeName = productList.filter((item) => item.code === params.code)[0]?.name || '';
        } else {
          params.code = selectTranding?.tradingCode;
          params.codeId = selectTranding?.secucode;
          params.codeName = selectTranding?.secuabbr || '';
        }
        params.file = params.fileList;
        delete params.fileList;
        return;
      }
    });
    return params;
  };
  // 文件上传成功
  onUploadFiles = (files) => {
    const {
      form: { setFieldsValue },
    } = this.props;
    setFieldsValue({
      fileList: files,
    });
  };
  // 资产代码变化
  tradingChange = (e) => {
    this.setState({
      selectTranding: e,
    });
  };
  isStandardChange = (e) => {
    this.setState({
      isStandard: e || undefined,
    });
  };
  render() {
    const { dimensionList, assetTypeList, assetDict, dimension, assetType, isStandard } = this.state;
    const {
      infoDetail,
      form,
      valuationAnalysisM: { productList = [] },
    } = this.props;
    const { getFieldDecorator } = form;

    const formProps = {
      form,
      rowNumber: 1,
      autoLayout: false,
      onRef: (ref) => (this.formGroupRef = ref),
      dataSource: [
        {
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'dimension',
          title: '解析维度',
          type: DATA_ENTRY.SELECT,
          dataSource: dimensionList,
          allowClear: true,
          initialValue: infoDetail?.dimension,
          disabled: !!infoDetail,
          onChange: (e) => this.formFieldChange('dimension', e),
          rules: [{ required: true, message: '请选择解析维度' }],
        },
        {
          hide: !(dimension == '1'),
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'code',
          title: '产品代码',
          initialValue: infoDetail?.code,
          type: DATA_ENTRY.SELECT,
          disabled: !!infoDetail,
          positionBody: true,
          dataSource: productList,
          filterOption: (input, option) => {
            //模糊搜索value 和 label
            return option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 || option.props.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
          },
          // rules: [{ required: true, message: '请选择产品' }],
          allowClear: true,
        },
        {
          hide: !(dimension == '2'),
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'assetType',
          title: '资产类型',
          type: DATA_ENTRY.SELECT,
          dataSource: assetTypeList,
          allowClear: true,
          initialValue: infoDetail?.assetType || assetType,
          disabled: !!infoDetail,
          positionBody: true,
          onChange: (e) => this.formFieldChange('assetType', e),
          rules: [{ required: true, message: '请选择资产类型' }],
        },
        {
          hide: !(dimension == '2'),
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'isStandard',
          title: '是否标准估值表',
          positionBody: true,
          type: DATA_ENTRY.SELECT,
          isTrueFalseJudge: true,
          initialValue: '1',
          rules: [{ required: true, message: '请选择是否标准估值表' }],
          onChange: this.isStandardChange,
          allowClear: false,
        },
        {
          hide: !(dimension == '2' && assetType == '01'), // 资产管理计划
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'code',
          title: '资产代码',
          type: DATA_ENTRY.SECURITIESCODE,
          codeType: codeType[assetDict[assetType]],
          initialValue: infoDetail?.code,
          disabled: !!infoDetail,
          positionBody: true,
          onChange: this.tradingChange,
          rules: [{ required: isStandard == '0' ? true : false, message: '请选择资产代码' }],
        },
        {
          hide: !(dimension == '2' && assetType == '02'), // 委外投资
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'code',
          title: '资产代码',
          type: DATA_ENTRY.SECURITIESCODE,
          codeType: codeType[assetDict[assetType]],
          initialValue: infoDetail?.code,
          disabled: !!infoDetail,
          positionBody: true,
          onChange: this.tradingChange,
          rules: [{ required: isStandard == '0' ? true : false, message: '请选择资产代码' }],
        },
        {
          hide: !(dimension == '2' && assetType == '03'), // 私募基金
          formItemLayoutDefine: FormItemLayout,
          dataIndex: 'code',
          title: '资产代码',
          type: DATA_ENTRY.SECURITIESCODE,
          codeType: codeType[assetDict[assetType]],
          initialValue: infoDetail?.code,
          disabled: !!infoDetail,
          positionBody: true,
          onChange: this.tradingChange,
          rules: [{ required: isStandard == '0' ? true : false, message: '请选择资产代码' }],
        },
      ],
    };
    return (
      <div style={{ overflow: 'hidden' }}>
        <FormGroup {...formProps} />
        <div className={styles.myUpload}>
          {isStandard === '1' ? (
            <Form.Item {...FormItemLayout} label='解析文件'>
              {getFieldDecorator('fileList', {
                valuePropName: 'fileList',
                rules: [{ required: true, message: '请上传文件' }],
              })(
                <UploadC
                  type={1}
                  showUploadList={true}
                  accept={'.xls,.XLS,.xlsx,.XLSX,.zip,.ZIP,.rar,.RAR'}
                  toolTip={'仅支持上传.xlsx，.xls，.zip，.rar'}
                  onRef={(ref) => (this.formUploadRef = ref)}
                  fileListChange={(files) => this.onUploadFiles(files)}
                  isDowm={false}
                  multiple={true}
                  // max={2}
                />,
              )}
            </Form.Item>
          ) : (
            <Form.Item {...FormItemLayout} label='解析文件'>
              {getFieldDecorator('fileList', {
                valuePropName: 'fileList',
                rules: [{ required: true, message: '请上传文件' }],
              })(
                <UploadC
                  type={1}
                  showUploadList={true}
                  accept={'.xls,.XLS,.xlsx,.XLSX'}
                  onRef={(ref) => (this.formUploadRef2 = ref)}
                  fileListChange={(files) => this.onUploadFiles(files)}
                  isDowm={false}
                  hasTemplate={true}
                  multiple={true}
                  downloadUrl={`${config.interfaces.type16}valuationanalysis/download`}
                  // max={2}
                />,
              )}
            </Form.Item>
          )}
        </div>
      </div>
    );
  }
}
